<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="codeId" slot-scope="scope">
        <el-tag v-if="scope.row.codeId===''">未实名</el-tag>
        <el-tag type="success" v-else>{{scope.row.codeId}}</el-tag>
      </template>
      <template slot="menuLeft">

      </template>
      <template slot-scope="scope" slot="menu">
        <el-popover
          placement="bottom"
          title="二次确认"
          width="200"
          :ref="'remark'+scope.row.$index"
          trigger="click">
          <p>确定清除该用户实名信息吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button type="primary" size="mini"  @click="clearIdCard(scope.row.id,scope.row.$index)">确定</el-button>
          </div>
          <el-button type="text"
                     icon="el-icon-circle-close"
                     size="small"
                     slot="reference"
          >清除实名
          </el-button>
        </el-popover>

        <el-button type="text"
                   icon="el-icon-notebook-2"
                   size="small"
                   @click="showBankcard(scope.row.userId)"
                   >绑卡信息
        </el-button>
      </template>
    </avue-crud>

    <el-dialog title="绑定银行卡"
               :visible.sync="bankcardShow"
               width="60%">

      <avue-crud :data="cardData"
                 :option="cardOption"
                 @on-load="cardOnLoad"
                 :table-loading="cardLoading"
                 ref="cardCrud"
                 @row-del="cardRowDel"
                 @search-change="cardSearchChange"
                 @search-reset="cardSearchReset"
                 @current-change="cardCurrentChange"
                 :page="cardPage"
                 @size-change="cardSizeChange">
        <template slot="menuLeft">
          <el-tag type="danger">您仅能在此删除银行卡信息</el-tag>
        </template>
      </avue-crud>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/pay/userpayinfo";
  import {mapGetters} from "vuex";
  import {getCardList,removeCard} from "@/api/pay/bankcard"
  export default {
    data() {
      return {
        //以下是银行卡配置信息
        cardQuery: {},
        bankcardShow:false,
        cardForm: {},
        cardLoading: true,
        cardPage: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        cardSelectionList: [],
        cardOption: {    excelBtn:true,
          editBtn: false,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          addBtn: false,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "卡号",
              prop: "bankCard",
              search:true,
              rules: [{
                required: true,
                message: "请输入卡号",
                trigger: "blur"
              }]
            },
            {
              label: "银行类型",
              dicUrl: "/api/ocm-system/dict/dictionary?code=bank_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              type: 'select',
              prop: "bankType",
              rules: [{
                required: true,
                message: "请输入银行类型",
                trigger: "blur"
              }]
            },
            {
              label: "银行卡类型",
              searchLabelWidth:100,
              prop: "cardType",
              dicUrl: "/api/ocm-system/dict/dictionary?code=card_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              type: 'select',
              rules: [{
                required: true,
                message: "请输入银行卡类型",
                trigger: "blur"
              }]
            },
            {
              label: "预留手机号",
              prop: "bankPhone",
              rules: [{
                required: true,
                message: "请输入预留手机号",
                trigger: "blur"
              }]
            },
            {
              label: "预留名",
              prop: "bankName",
              rules: [{
                required: true,
                message: "请输入预留名",
                trigger: "blur"
              }]
            },
          ]
        },
        cardData: [],
        //以下是实名信息及余额
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {    excelBtn:true,
          dialogTop: '100',
          delBtn: false,
          height: 'auto',
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "用户ID",
              prop: "userId",
              search:true,
              rules: [{
                required: true,
                message: "请输入用户id",
                trigger: "blur"
              }]
            },
            {
              label: "身份证号",
              prop: "codeId",
              editDisplay:false,
              search:true,
              rules: [{
                required: true,
                message: "请输入身份证号",
                trigger: "blur"
              }]
            },
            {
              label: "姓名",
              search:true,
              editDisplay:false,
              prop: "codeName",
              rules: [{
                required: true,
                message: "请输入实名人",
                trigger: "blur"
              }]
            },
            {
              label: "余额",
              prop: "balance",
              rules: [{
                required: true,
                message: "请输入余额",
                trigger: "blur"
              }]
            },
            {
              label: "冻结金额",
              prop: "freeze",
              rules: [{
                required: true,
                message: "请输入冻结金额",
                trigger: "blur"
              }]
            },
            {
              label: "积分",
              prop: "credit",
              rules: [{
                required: true,
                message: "请输入积分",
                trigger: "blur"
              }]
            },
            {
              label: "支付状态",
              prop: "status",
              dicUrl: "/api/ocm-system/dict/dictionary?code=pay_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              type: 'select',
              rules: [{
                required: true,
                message: "请选择支付状态",
                trigger: "click"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.userpayinfo_add, false),
          viewBtn: this.vaildData(this.permission.userpayinfo_view, false),
          delBtn: this.vaildData(this.permission.userpayinfo_delete, false),
          editBtn: this.vaildData(this.permission.userpayinfo_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      showBankcard(id){
        this.cardQuery={userId:id}
        this.bankcardShow=true
      },
      //清除实名信息
      clearIdCard(id,index){
        update({id:id,codeId:'',codeName:''}).then(() => {
          this.$refs['remark'+index].doClose()
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          this.$refs['remark'+index].doClose()
          window.console.log(error);
        });
      },
      //以下为对银行卡信息增删改查
      cardRowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      // cardRowUpdate(row, index, done, loading) {
      //   update(row).then(() => {
      //     done();
      //     this.cardOnLoad(this.cardPage);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!"
      //     });
      //   }, error => {
      //     window.console.log(error);
      //     loading();
      //   });
      // },
      cardRowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return removeCard(row.id);
          })
          .then(() => {
            this.cardOnLoad(this.cardPage);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },

      cardSearchReset() {
        this.cardQuery = {};
        this.cardOnLoad(this.cardPage);
      },
      cardSearchChange(params, done) {
        this.cardQuery = params;
        this.cardPage.currentPage = 1;
        this.cardOnLoad(this.cardPage, params);
        done();
      },
      cardCurrentChange(currentPage){
        this.cardPage.currentPage = currentPage;
      },
      cardSizeChange(pageSize){
        this.cardPage.pageSize = pageSize;
      },
      cardOnLoad(page, params = {}) {
        this.cardLoading = true;
        getCardList(page.currentPage, page.pageSize, Object.assign(params, this.cardQuery)).then(res => {
          const data = res.data.data;
          this.cardPage.total = data.total;
          this.cardData = data.records;
          this.cardLoading = false;
        });
      },
      //以下是实名信息增删改查
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      tt(){
        alert("ww")
      }
    }
  };
</script>

<style>
</style>
